package com.hsck.order.kds.setup.fragment.cxzd

import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import com.hjq.toast.Toaster
import com.hsck.order.kds.R
import com.hsck.order.kds.base.bean.CategoryBean
import com.hsck.order.kds.base.isnoam.network.livedata.LiveDataBus
import com.hsck.order.kds.base.isnoam.view.BaseFragment
import com.hsck.order.kds.databinding.FragmentGldkBinding
import com.hsck.order.kds.setup.SetupActivity
import com.hsck.order.kds.setup.adapter.GldkAdapter
import kotlinx.android.synthetic.main.common_head_black.*


/**
 * 关联档口
 */
class GldkFragment : BaseFragment<FragmentGldkBinding>(), SetupActivity.UpdatableFragment {


    var type = ""
    var topFragment: Fragment? = null
    private val adapter by lazy { GldkAdapter(R.layout.item_gldk) }


    override fun inflateBinding(
        inflater: LayoutInflater,
        container: ViewGroup?
    ): FragmentGldkBinding {
        return FragmentGldkBinding.inflate(inflater)
    }

    fun setBack() {
        if (topFragment != null) {
            val activity = requireActivity() as SetupActivity
            activity.switchFragment(topFragment!!)
        }
    }

    override fun loadDataOnce() {
        super.loadDataOnce()
        lin_head_black_back.setOnClickListener {
            setBack()
        }
        tv_head_black_title.text = "关联档口"
        binding.apply {
            rvGldk.layoutManager = GridLayoutManager(requireActivity(), 2)
            rvGldk.adapter = adapter
            adapter.setMultipleChoice(R.id.cb_item_gldk, true)
            val list = arrayListOf<CategoryBean>()
            for (i in 1..20) {
                list.add(CategoryBean("酒水$i", false))
            }
            adapter.datas = list
            adapter.onItemClick = {
                val listSelect = adapter.getSelect() as ArrayList<CategoryBean>
                cbGldkAll.isChecked = listSelect.size == list.size
            }

            //全选
            linGldkAll.setOnClickListener {
                if (adapter.datas.isNotEmpty()) {
                    if (cbGldkAll.isChecked) {
                        list.forEach {
                            it.isSelected = false
                        }
                    } else {
                        list.forEach {
                            it.isSelected = true
                        }
                    }
                    cbGldkAll.isChecked = !cbGldkAll.isChecked
                    adapter.notifyDataSetChanged()
                } else {
                    cbGldkAll.isChecked = false
                }
            }
            //确认
            btnGldkQr.setOnClickListener {
                val list = adapter.getSelect() as ArrayList<CategoryBean>
                //给上级页面传值
                LiveDataBus.with<ArrayList<CategoryBean>>("GldkFragment").postData(list)
                setBack()
            }

        }
    }

    override fun onFragmentVisible() {
        //可见
        if (_binding != null) {

        }
    }

    override fun onFragmentInvisible() {
        //不可见

    }

}